
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xreader/common/novel_helper.dart';
import 'package:xreader/logs/log.dart';
import 'package:xreader/novel/entity/entity_novel_book_info.dart';
import 'package:xreader/novel/view/reader/novel_reader_list_item_of_chapter.dart';
import 'package:xreader/novel/viewmodel/novel_content_view_model.dart';
import 'package:xreader/reader/widget/novel_reader_loading_widget.dart';
import 'package:xreader/reader/widget/scroll/power_scroll_view.dart';

/// 小说阅读器整体部分
/// 这部分负责处理目录和对应章节的管理
/// 具体章节内容，通过当前章节对应的index,从目录中获取信息，并传给NovelListChapterItem,由其处理具体章节内容展示
class NovelReaderListPage extends StatelessWidget{
  final int bookid;
  final int chapterId;
  const NovelReaderListPage(this.bookid,this.chapterId,{Key? key}):super(key : key);
  @override
  Widget build(BuildContext context) {
    Log.i("bookid=====>${bookid}");
    NovelContentChapterViewModel testViewModel = Get.find<NovelContentChapterViewModel>(tag:"${bookid}");
    Log.i("testViewModel=====>${testViewModel}");
    return Scaffold(
      body: Container(
        child: GetBuilder(
          init: Get.find<NovelContentChapterViewModel>(tag:"${bookid}"),
          builder: (NovelContentChapterViewModel viewModel){
            var value = viewModel.currentNovelInfo;
            var turnPageMode = viewModel.currentTurnMode;
            if(value.value == null || (value.value!.chapterList?.isEmpty ?? false)){
              return NovelReaderLoadingWidget();
            }
            List<NovelChapterInfo> chapterList = value.value!.chapterList!;
            var initialPageIndex = 1;
            return PowerListView.builder(
              key: ValueKey(turnPageMode),
              physics: PageScrollPhysics(),
              // controller: controller,
              controller: buildNovelScrollController(turnPageMode),
              // controller: PowerListScrollSimulationController(),
              addRepaintBoundaries:
              turnPageMode != ReaderTurnPageMode.simulationMode,
              scrollDirection: Axis.horizontal,
              layoutManager: buildNovelLayoutManager(turnPageMode),
              debugTag: 'outerParent',
              itemBuilder: (BuildContext context, int _index) {
                return NovelListChapterItem(
                  novelChapterInfo: chapterList[_index],
                  currentChapterIndex: initialPageIndex,
                  turnMode: turnPageMode,
                );
                // return buildTestContentItem(constraints, _index);
              },
              itemCount: chapterList.length,
            );
          },
        ),
      ),
    );
  }

}